package com.herench.bs.handler;

import com.herench.bs.agent.IPersonAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 手机注册（不包含个人信息）
 * cleverClinic
 * com.herench.bs.handler
 *
 * @author wangwenkai
 * @date 2018/1/12/10:40
 */
@Component
@Handler(code = "100200" ,name="手机注册（不包含个人信息）")
public class PhoneRegisterHandler extends AbstractMessageHandler {
    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        Map<String, Object> res = new HashMap<>();

        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String phone = requestMessage.getArgsPropertyStringValue("phone");
        String verificationCode = requestMessage.getArgsPropertyStringValue("verifyCode");
        String password = requestMessage.getArgsPropertyStringValue("password");
        String registerChannel = requestMessage.getArgsPropertyStringValue("fromProduct");
        String registerProtocol = requestMessage.getArgsPropertyStringValue("registerProtocol");
        String appImei = requestMessage.getArgsPropertyStringValue("appImei");
        String webchat = requestMessage.getArgsPropertyStringValue("webchat");

        Assert.isTrue(StringUtils.hasLength(phone), "phone 不能为空.");
        Assert.isTrue(StringUtils.hasLength(password), "password 不能为空.");
 //       Assert.isTrue(StringUtils.hasLength(verificationCode), "verifyCode 不能为空.");
        Assert.isTrue(StringUtils.hasLength(channelCode), "sysCode 不能为空.");
        Assert.isTrue(StringUtils.hasLength(registerChannel), "fromProduct 不能为空.");

        responseMessage = new ResponseMessage(requestMessage.getId());
        IPersonAgent personAgent = this.getCleverClinicContext().getApplicationContext().getBean(IPersonAgent.class);
        res = personAgent.phoneRegister(phone, password, verificationCode, channelCode, registerChannel, appImei, webchat);
        responseMessage.setDataAttribute("fromProduct", registerChannel);
        responseMessage.setDataAttribute("phone", phone);
        responseMessage.setDataAttribute("registerProtocol", registerProtocol);
        responseMessage.setDataAttribute("sysCode", channelCode);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
